package com.block.sms.service.send;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.common.util.LocalDateUtil;
import com.block.db.entity.SmsConfigInfo;
import com.block.db.entity.SmsTemplateInfo;
import com.block.sms.dto.*;
import com.block.sms.service.ISmsSendService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class WeiTanSmsSendServiceImpl implements ISmsSendService {


    private static final Logger log = LogFactory.getCommLog();

    @Override
    public SmsResultDTO sendSms(SmsParamDTO smsParamDTO, SmsTemplateInfo smsTemplate, SmsConfigInfo smsConfigInfo) {
        SmsResultDTO smsResultDTO = new SmsResultDTO();

        //获取当前时间戳
        String timestamp = LocalDateUtil.getNowDateStr(LocalDateUtil.TOTAL_DATE_TIME_NOT_SPACE_GANG);

        //格式化短信内容
        JSONArray paramArr = smsParamDTO.getParamArray();
        String content = StrUtil.format(smsTemplate.getTemplateContent(), paramArr.stream().toArray());

        //构建签名
        String sign = SecureUtil.md5(smsConfigInfo.getAccountSecret() + smsConfigInfo.getAccountKey() + smsParamDTO.getMobie()+ content).toUpperCase();

        //构建请求参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("action","p2p");
        paramMap.put("account",smsConfigInfo.getAccountNo());
        //用md5签名作为密码
        paramMap.put("password",sign);
        //接入码
        paramMap.put("extno",smsConfigInfo.getAccountKey());
        
        //构建号码内容键值对
        HashMap<String, String> mobileContentKvp = new HashMap<>();
        mobileContentKvp.put(smsParamDTO.getMobie(),content);
        paramMap.put("mobileContentKvp",mobileContentKvp);

        //构建post请求
        String bodyResult = HttpUtil.createPost(smsConfigInfo.getSendUrl())
                .body(JSON.toJSONString(paramMap))
                .execute()
                .body();
        //日志打印
        log.info("【微探短信服务】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
                smsConfigInfo.getSendUrl(), JSONUtil.toJsonStr(paramMap), bodyResult);

        //解析响应结果
        JSONObject resultJson = JSON.parseObject(bodyResult);
        Integer status = resultJson.getInteger("status");
        JSONArray list = resultJson.getJSONArray("list");

        smsResultDTO.setResult(bodyResult);
        smsResultDTO.setCode(String.valueOf(status));

        if (!ObjectUtil.equals(Integer.valueOf("0"), status)) {
            smsResultDTO.setSuccess(false);
            return smsResultDTO;
        }

        JSONObject jsonObject = list.getJSONObject(0);
        Integer result = jsonObject.getInteger("result");

        if (!ObjectUtil.equals(Integer.valueOf("0"), result)) {
            smsResultDTO.setSuccess(false);
            return smsResultDTO;
        }
        smsResultDTO.setSuccess(true);

        return smsResultDTO;
    }

    @Override
    public SmsBalanceDTO queryBalanceInfo(SmsConfigInfo smsConfigInfo) {
        return null;
    }

    @Override
    public QueryResultDTO querySendResult(QueryParamDTO queryParamDTO, SmsConfigInfo smsConfigInfo) {
        return null;
    }
}
